package com.twu.biblioteca.models;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: ema93sh
 * Date: 8/16/13
 * Time: 9:01 PM
 */
public class User {
    // Split the username into two parts userid and loginid
    public static int nextUserId = 111;
    public static int nextLoginId = 1111;
    public int userId;
    public int loginId;
    private String password;
    private String email;
    private String name;
    private String phoneNo;
    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";



    public static User createUser(String password) {
        User user = new User(nextUserId,nextLoginId, password);
        generateNextUsername();
        return user;
    }

    private static void generateNextUsername() {
        if ( nextLoginId == 9999)
        {
            nextUserId += 1;
            nextLoginId = 0;
        }
        nextLoginId += 1;
    }

    public User(int userId, int loginId, String password)
    {
        this.userId = userId;
        this.loginId = loginId;
        this.password = password;
    }

    public String getUsername() {
        StringBuilder builder = new StringBuilder();
        builder.append(userId);
        builder.append("-");
        builder.append(loginId);
        return userId + "-" + loginId;
    }


    public void setPassword(String password) {
        this.password = password;
    }

    public boolean checkPassword(String password) {
        return this.password.equals(password);
    }

    public void setEmail(String email) throws Exception {
        if (isValidEmail(email))
            this.email = email;
        else {
            throw new Exception("Invalid Email");
        }
    }

    private boolean isValidEmail(String email) {
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public String getEmail() {
        return email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPhoneNo(String phoneNo) throws Exception {
        if (isValidPhoneNo(phoneNo))
            this.phoneNo = phoneNo;
        else
            throw new Exception("Invalid Phone Number");
    }

    private boolean isValidPhoneNo(String phoneNo) {
        String regex = "[0-9]+";
        return phoneNo.matches(regex);
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public Dictionary<String, String> getUserDetails() {
        Dictionary<String, String> details = new Hashtable<String, String>();
        if (getName() != null)
            details.put("Name", getName());
        if (getEmail() != null)
            details.put("Email", getEmail());
        if (getPhoneNo() != null)
            details.put("Phone No", getPhoneNo());
        return details;
    }
}
